package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 三级菜单展示
     *
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData() {
        //找出第一级菜单
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getPid, 0);
        List<Menu> list = menuService.list(queryWrapper);

        //找出第二级菜单
        List<MenuDto> menuDtos = list.stream().map(item -> {
            MenuDto menuDto = new MenuDto();
            BeanUtils.copyProperties(item, menuDto);
            LambdaQueryWrapper<Menu> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Menu::getPid, item.getId());
            List<Menu> list1 = menuService.list(queryWrapper1);
            //遍历2级菜单,复制菜单到dto中,二级菜单中包含三级菜单
            List<MenuDto> list3 = list1.stream().map(twoMenu -> {
                MenuDto menuDtoTwo = new MenuDto();
                BeanUtils.copyProperties(twoMenu, menuDtoTwo);
                LambdaQueryWrapper<Menu> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(Menu::getPid, twoMenu.getId());
                List<Menu> list2 = menuService.list(queryWrapper2);
                menuDtoTwo.setChildren(list2);
                return menuDtoTwo;
            }).collect(Collectors.toList());

            menuDto.setChildren(list3);
            return menuDto;
        }).collect(Collectors.toList());

        return ResultData.ok("", menuDtos);

    }

    /**
     * 修改回显
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
    /*    LambdaQueryWrapper<Menu> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getId,id);*/
        Menu menu = menuService.getById(id);
        return ResultData.ok("", menu);
    }

    /**
     * 修改功能
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return ResultData.ok("修改成功", null);
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody Menu menu) {
        menu.setCreate_date(LocalDateTime.now());
        menuService.save(menu);
        return ResultData.ok("新增成功", null);
    }

    @GetMapping("/del")
    public ResultData del(Integer id) {
        //先查询是否有子菜单
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getPid, id);
        List<Menu> list = menuService.list(queryWrapper);
        if (list != null && list.size() != 0) {
            throw new BusinessException("该菜单下面有子菜单,不能删除");
        }
        //该菜单是否被用户使用
        LambdaQueryWrapper<RoleMenu> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RoleMenu::getMenu_id, id);
        List<RoleMenu> list1 = roleMenuService.list(queryWrapper1);
        if (list1 != null && list1.size() != 0) {
            throw new BusinessException("该菜单已被使用,不能删除");
        }
        //可以删除
        menuService.removeById(id);

        return ResultData.ok("删除成功", null);
    }


}
